<template>
  <div :class="className" :style="{height:height,width:width}" ref="chartDiv"/>
</template>

<script>
  import * as echarts from 'echarts'
  require('echarts/theme/royal') // echarts theme
  import {debounce} from '@/utils'
  import html2canvas from "html2canvas";

  export default {
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      optionTitleText: {
        type: String,
        default: ''
      },
      optionSeries: {
        type: Array,
        default: []
      },
      optionLegend: {
        type: Array,
        default: []
      },
      height: {
        type: String,
        default: '300px'
      },
      saveImage: {
        type: Boolean,
        default: false
      },
    },
    data() {
      return {
        base64Image: undefined,
        chart: null,
        opinion: {
          title: {
            text: this.optionTitleText,
            padding: 10,
          },
          backgroundColor: '#fff',
          tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
          },
          legend: {
            left: 'center',
            bottom: '10',
            data: this.optionLegend
          },
          calculable: true,
          series: this.optionSeries
        }
      }
    },
    mounted() {
      this.initChart()
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      window.removeEventListener('resize', this.__resizeHandler)
      this.chart.dispose()
      this.chart = null
    },
    watch: {
      optionSeries(newValue, oldValue) {
        this.opinion.series = newValue
        this.chart.setOption(this.opinion);
      },

      optionLegend(newValue, oldValue) {
        this.opinion.legend.data = newValue
        this.chart.setOption(this.opinion);
      },
      optionTitleText(newValue, oldValue) {
        this.opinion.title.text = newValue
        this.chart.setOption(this.opinion);
      },
    },
    methods: {
      doSaveImage() {
        let element = this.$refs.chartDiv
        const p1 = new Promise((resolve, reject) => {
          html2canvas(element).then(canvas => {
            this.base64Image = canvas.toDataURL("image/png")
            this.$emit("getBase64Image", this.base64Image)
            resolve()
          }).catch(error => {
            reject(error)
          });
        })
        const p = Promise.all([p1]);
        p.then()

      },
      initChart() {
        //定义一个promise对象 在内部执行我们的表格渲染代码
        let promise = new Promise((resolve, reject) => {

          this.chart = echarts.init(this.$el, 'royal')
          this.chart.setOption(this.opinion)
          // 绑定渲染停止时触发promise完成状态
          this.chart.on('finished', () => {
            resolve();
          })
          this.__resizeHandler = debounce(() => {
            if (this.chart) {
              this.chart.resize()
            }
          }, 100)
          window.addEventListener('resize', this.__resizeHandler)
        });
        // 渲染完成后执行then()方法的操作代码
        promise.then(() => {
          if (this.saveImage) {
            console.log("开始保存图片")
            this.doSaveImage();
          }
        });
      }
    }
  }
</script>
